﻿<?php 
class Adminmodel extends CI_Model{
	function __construct(){
    parent::__construct();
		$this->load->database();
		$this->load->library(array('session','pagination'));
		$this->load->helper('url');

    }
	function get_user(){
		$sql="select * from user";
		$query=$this->db->query($sql);
		return $query->result();
		
		
	}   //导航操作
	function get_category(){
		$sql="select * from category";
		$query=$this->db->query($sql);
		return $query->result_array();
		
		
	}
	
	
	
	
	
	/*
	*
	*文章操作
	*
	*
	*/
	function get_article_rows($cid){
		$sql="select * from article where cid='$cid'";
		$query=$this->db->query($sql);
		return $query->num_rows();

	}
	function get_article_list($cid,$lim,$perpage){
		$query=$this->db->query("select * from article where cid='$cid' limit $lim,$perpage");
		return $query->result_array();
		
	}
    function addarticle($url1){
		$cid=$this->input->post('cid');
		$sql="select * from category where cid='$cid'";
		$arr=$this->db->query($sql)->row_array();
		$class=$arr['name'];
	    $title=$this->input->post('title');
		$author=$this->input->post('author');
		$time=$this->input->post('time');
		$content=$this->input->post('content');
		$settop=$this->input->post('settop');
		$url=$url1;
		$data=array(
		'title'=>$title,
		'author'=>$author,
		'time'=>$time,
		'content'=>$content,
		'url'=>$url,
		'settop'=>$settop,
		'class'=>$class,
		'cid'=>$cid
		
		);
		$this->db->insert('article',$data);
		if($this->db->affected_rows()>0){
			return 1;
		}else {
			return 0;
		}
		
	}
	function editarticle($id){
		$sql="select * from article where id='$id'";
		$query=$this->db->query($sql);
		return $query->result_array();	//返回数组
	}
	
	function updatearticle(){
            $id=$this->uri->segment(3);
			$title=$this->input->post('title');
			$author=$this->input->post('author');
			$time=$this->input->post('time');
			$content=$this->input->post('content');
			$data=array(
			    'title'=>$title,
				'author'=>$author,
				'time'=>$time,
				'content'=>$content,
			);

			$this->db->where('id',$id);
			$this->db->update('article',$data); 	
	
	        if($this->db->affected_rows()>0){
				return 1;
			}else{
				return 0;
			}
	}
	function settop(){
		$id=$this->uri->segment(3);
		$cid=$this->uri->segment(4);
		$this->db->where('id',$id);
		$data=array('settop'=>1);
		$this->db->update('article',$data);
		$sql="update article set settop=0 where id!='$id' and cid='$cid' ";
		$result=$this->db->query($sql);
	 	if($this->db->affected_rows()>0){
			return 1;
		}else {
			return 1;
		}  //永远返回1
		
		
	}
	function cancelsettop(){
		$id=$this->uri->segment(3);
		$this->db->where('id',$id);
		$data=array('settop'=>0);
		$this->db->update('article',$data);
		if($this->db->affected_rows()>0){
			return 1;
		}else {
			return 0;
		}
		
		
	}
	function delarticle($id){
		$this->db->where('id',$id);
		$this->db->delete('article');
		if($this->db->affected_rows()>0){
			return 1;
		}else {
			return 0;
		}
		
	}
	
	
	
	/*
	*
	*评论操作
	*
	*
	*/
		// function getcomment(){
			// $id=$this->uri->segment(3);
			// $sql="select * from comment where id='$id'";
			// $query=$this->db->query($sql);
			// return $query->result_array();
			
		// }
		function get_comment_rows($id){
			$sql="select * from comment where id='$id'";
			$query=$this->db->query($sql);
			return $query->num_rows();

		}
		function getcomment($id,$lim,$perpage){
			
			$query=$this->db->query("select * from comment where id='$id' limit $lim,$perpage");
			return $query->result_array();
		
		}
		function delcomment(){
			$cid=$this->uri->segment(3);
		    $this->db->where('cid',$cid);
			$this->db->delete('comment');
			if($this->db->affected_rows()>0){
			    return 1;
			}else{
				return 0;
			}
			
			
		}
	
	
	/*
	 * 
	 * 文件操作
	 * 
	 * 
	 * 
	 * 
	 */
	
	function get_file_rows($cid){
		$query=$this->db->query("select * from file_img where cid='$cid'");
		return $query->num_rows();
	}
	
	function get_file_list($cid,$lim,$per_page){
		$query=$this->db->query("select * from file_img where cid='$cid' order by id desc limit $lim,$per_page");
		return $query->result();
	}
	
	function add_file($array){
// 		$cid=$array[0];
// 		$name=$array[1];
// 		$url=$array[2];
// 		$time=$array[3];
		$data=array(
				'cid'=>$array[0],
				'name'=>$array[1],
				'url'=>$array[2],
				'time'=>$array[3]		
		);
		if($this->db->insert("file_img",$data)){
			$flag=true;
		}else{
			$flag=false;
		}
		return $flag;
	}
	
	function get_file_content($id){
		$query=$this->db->query("select * from file_img where id='$id'");
		return $query->result();
	}
	
	function no_edit_file(){   //只改名字，未上传文件
		$id=$this->input->post("id");
		$name=$this->input->post("name");
		$time=$this->input->post("time");
		$data=array('name'=>$name,'time'=>$time);
		$this->db->where("id",$id);
		if($this->db->update("file_img",$data)){
			$flag=true;
		}else{
			$flag=false;
		}
		return $flag;
	}
	
	function edit_file($array){
		$id=$this->input->post("id");
		$data=array(
				'name'=>$array[0],
				'time'=>$array[1],
				'url'=>$array[2]		
		);
		$this->db->where('id',$id);
		if($this->db->update("file_img",$data)){
			$flag=true;
		}else{
			$flag=false;
		}
		return $flag;
	}
	
	function get_file_cid($id){
		$query=$this->db->query("select * from file_img where id='$id'");
		foreach ($query->result() as $value){
			$cid=$value->cid;
		}
		return $cid;
	}
	
	function del_file($id){
		$this->db->where("id",$id);
		if($this->db->delete("file_img")){
			$flag=true;
		}else{
			$flag=false;
		}
		return $flag;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}




?>